#include "private/jicmpsocket_p.h"

namespace QTE
{

JICMPSocket::JICMPSocket(QObject *parent) :
	QObject(parent), d_ptr(new JICMPSocketPrivate(this))
{

}

JICMPSocket::JICMPSocket(quint64 timeout, QObject *parent) :
	JICMPSocket(parent)
{
	setTimeout(timeout);
}

JICMPSocket::~JICMPSocket()
{

}

int JICMPSocket::ping(const QString &hostName, int cnt)
{
	if( hostName.isEmpty() )
	{
		d_ptr->m_errorString = "Host ip or domain name is invalid. (empty)";
		jWarning() << "Host ip or domain name is invalid. (empty)";
		return -1;
	}

	d_ptr->m_errorString = "";
	QHostAddress qaddr;

	if( qaddr.setAddress(hostName) )
		return d_ptr->ping(qaddr, cnt);
	return d_ptr->ping(hostName, cnt);
}

int JICMPSocket::ping(const QHostAddress &address, int cnt)
{
	if( address.isNull() )
	{
		d_ptr->m_errorString = "Host address is invalid.";
		jWarning() << "Host address is invalid.";
		return -1;
	}

	d_ptr->m_errorString = "";
	return d_ptr->ping(address, cnt);
}

void JICMPSocket::setTimeout(quint64 msecs)
{
	if( msecs == 0 )
		d_ptr->m_timeout = 1;
	else
		d_ptr->m_timeout = msecs;
}

quint64 JICMPSocket::timeout() const
{
	return d_ptr->m_timeout;
}

int JICMPSocket::waitForPong(PingResponse &response, int timeout)
{
	d_ptr->m_errorString = "";
	return d_ptr->waitForPong(response, timeout);
}

QString JICMPSocket::errorString() const
{
	return d_ptr->m_errorString;
}

void JICMPSocket::setErrorString(const QString &errstr)
{
	d_ptr->m_errorString = errstr;
}

static int __register_type()
{
	qRegisterMetaType<QTE::JICMPSocket::PingResponse>("QTE::JICMPSocket::PingResponse");
	qRegisterMetaType<JICMPSocket::PingResponse>("JICMPSocket::PingResponse");
	return 0;
}
static __attribute_used__ int _r = __register_type();

} //namespace QTE
